define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/index' + location.search,
                    add_url: 'activity/add',
                    edit_url: 'activity/edit',
                    del_url: 'activity/del',
                    multi_url: 'activity/multi',
                    import_url: 'activity/import',
                    table: 'activity',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%","100%"]);
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone,.btn-edit").data("area", ["100%", "100%"]);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'admin.nickname', title: __('Admin_id'),operate: false},
                        {field: 'type', title: __('Type'),formatter: function (value) {
                                return value == 1?'优惠券':'转账零钱';
                            },searchList:{1:'优惠券',2:'转账零钱'}},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'banner', title: __('Banner'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'discount_coupon', title: __('券'), operate: false, formatter: function (value) {
                                var str='';
                                value.forEach(function(item) {
                                    str += '支付金额:'+item['pay_money']+' 优惠金额:'+item['discounts_money']+' 数量:'+item['number']+"<br/>";
                                });
                                return str;
                            }},
                        {field: 'form.name', title: __('Form.name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),formatter: function (value) {
                                return value == 1?'正常':'禁用';
                            },searchList:{1:'正常',2:'禁用'}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'import_basic_data',
                                    text: '导入白名单',
                                    title: '导入白名单',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-upload',
                                    url: 'basic_data/import_basic_data',
                                    hidden:function (data){
                                        if(data.pid == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: '白名单列表',
                                    text: '白名单列表',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-folder-o',
                                    url: function(v,r,i){
                                        return 'basic_data/index?activity_id='+v.id
                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('input[type=radio][name="row[type]"]').change(function () {
                var value = $(this).val();
                console.log(value)
                if (value == 2) {
                    $('.batch_no').hide();
                    $('.pay_money').hide();
                } else {
                    $('.batch_no').show();
                    $('.pay_money').show();
                }
            });
            Template.helper("Fast", Fast);
            //因为日期选择框不会触发change事件，导致无法刷新textarea，所以加上判断
            $(document).on("dp.change", "#second-form .datetimepicker", function () {
                $(this).parent().prev().find("input").trigger("change");
            });
            $(document).on("fa.event.appendfieldlist", "#first-table .btn-append", function (e, obj) {

            });
            $(document).on("fa.event.appendfieldlist", "#second-table .btn-append", function (e, obj) {
                //绑定动态下拉组件
                Form.events.selectpage(obj);
                //绑定日期组件
                Form.events.datetimepicker(obj);
                //绑定上传组件
                Form.events.faupload(obj);

                //上传成功回调事件，变更按钮的背景
                $(".upload-image", obj).data("upload-success", function (data) {
                    $(this).css("background-image", "url('" + Fast.api.cdnurl(data.url) + "')");
                })
            });
            Controller.api.bindevent();
        },
        edit: function () {
            setTimeout(function() {
                if (type == 2) {
                    $('.batch_no').hide();
                    $('.pay_money').hide();
                } else {
                    $('.batch_no').show();
                    $('.pay_money').show();
                }
            }, 2000); // 3000 毫秒 = 3 秒
            $('input[type=radio][name="row[type]"]').change(function () {
                var value = $(this).val();
                console.log(value)
                if (value == 2) {
                    $('.batch_no').hide();
                    $('.pay_money').hide();
                } else {
                    $('.batch_no').show();
                    $('.pay_money').show();
                }
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
